#include "ezvoiceplayermanager.h"
#include "normalplaylistmodel.h"
#include "stream/filestream.h"
#include "log/EZQLOG.h"
#include "stream/filestream.h"
#include "stream/httpstream.h"
#include "songBll/songdownloadhttp.h"

EZVoicePlayerManager* EZVoicePlayerManager::defaultManager()
{
    static EZVoicePlayerManager *manager = 0;
    if(manager==0){
        manager = new EZVoicePlayerManager();
    }
    return manager;
}

EZVoicePlayerManager::EZVoicePlayerManager(QObject *parent):
    QObject(parent),playList(0)
{
    _audioPlayer = EZQAudioPlayer::sharePlayer();
    _willPlayOnSongChange = false;
    connect(_audioPlayer,SIGNAL(sigStausChanged(EZQAudioPlayStatus)),this,SLOT(onAudioPlayerStatusChange(EZQAudioPlayStatus)),Qt::QueuedConnection);
//    connect(player,SIGNAL(sigstateChanged(EZVoiceState)),this,SLOT(onPlayStatusChange(EZVoiceState)));
}

EZVoicePlayerManager::~EZVoicePlayerManager()
{
    if(this->playList){
        this->playList->Release();
        this->playList = 0;
    }
//    this->player = 0;
    this->_audioPlayer = 0;
}

//void EZVoicePlayerManager::onPlayStatusChange(EZVoiceState state)
//{
//    if(state.playState ==EZVoicePlayerState_stop &&
//            (state.mediaState == EZVoiceMediaState_end|| state.mediaState==EZVoiceMediaState_error)
//            ){
//        playNext();
//        qDebug() << "playNext .... .";
//    }
//}
void EZVoicePlayerManager::onAudioPlayerStatusChange(EZQAudioPlayStatus status)
{
//    EZDebug << "onAudioPlayerStatusChange" << status;
    if(status==EZQAudioPlayStatus_finish){
        playNext();
    }else if(status == EZQAudioPlayStatus_error){
        playNext();
    }
}

PlayListBaseModel* EZVoicePlayerManager::getPlayList()
{
    return this->playList;
}

void EZVoicePlayerManager::setPlayList(PlayListBaseModel *value)
{
    if(value){
        value->addDelegate(this);
        connect(value,SIGNAL(sigPlayQueueTypeChange()),this,SLOT(onPlayListQueueTypeChange()));
        value->Retain();
    }
    if(this->playList){
        this->playList->removeDelegate(this);
        disconnect(this->playList,SIGNAL(sigPlayQueueTypeChange()),this,SLOT(onPlayListQueueTypeChange()));
        this->playList->Release();
    }
    this->playList = value;
    emit sigPlayListContentChange();
}

void EZVoicePlayerManager::setPlayList(EZTally::EZTallyArray<SongModel*> *songs)
{
    NormalPlayListModel *newPlayList = new NormalPlayListModel();
    newPlayList->setSongs(songs);
    setPlayList(newPlayList);
}

void EZVoicePlayerManager::playOrPause()
{
//    player->playOrPause();
    _audioPlayer->playOrPause();
}

void EZVoicePlayerManager::playIndex(int index)
{
    if(playList==0){
        return;
    }
    SongModel *song = playList->songAtIndex(index);
    playSong(song,index);
}
void EZVoicePlayerManager::playNext()
{
    EZDebug << "playNext";
    if(playList==0){
        return;
    }
    int index = 0;
    SongModel *song = playList->nextSong(&index);
    playSong(song,index);
}

void EZVoicePlayerManager::playCurrent()
{
    if(playList==0){
        return;
    }
    SongModel *song = playList->curSong();
    playSong(song,playList->getCurIndex());
}

void EZVoicePlayerManager::playPrevious()
{
    if(playList==0){
        return;
    }
    int index = 0;
    SongModel *song = playList->previoursSong(&index);
    playSong(song,index);

}

void EZVoicePlayerManager::playSong(SongModel *song,int index)
{
    if(song==0){
        _willPlayOnSongChange = true;
        return;
    }
    _willPlayOnSongChange = false;
//    player->playSong(song);
    StreamBase *stream = 0;
//    QString path = song->getUrlWith(songHash_localPath,0);
    QString localkey(songHash_localPath);
    SongModel::Quality localQuali = song->qualityWithKey(localkey);
    if(localQuali.url.length() > 0){
        stream = new FileStream(localQuali.url);
    }else{
        SongDownLoadHttp *http = new SongDownLoadHttp();
        http->setSong(song);
        stream = new HttpStream(http);
        http->Release();
    }
    _audioPlayer->setStream(stream);
    _audioPlayer->play();
    if(stream){
        stream->Release();
    }

    if(index<0 || index>playList->count()){
        index = playList->indexOfSong(song);
    }
    playList->setCurIndex(index);
    emit sigPlayIndexChange(index);

}

void EZVoicePlayerManager::onSongChange(PlayListBaseModel *sender,EZTally::EZTallyArray<SongModel*> */*oldSong*/)
{
    if(sender != playList){
        return;
    }
    if(_willPlayOnSongChange==true){
        playCurrent();
    }
    emit sigPlayListContentChange();
}

void EZVoicePlayerManager::onPlayListQueueTypeChange()
{
    emit sigPlayQueueTypeChange();
}
